package com.stnet.dao.enterprice.chinamobile;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.modules.commons.Pager;
import com.stnet.bases.BaseDaoImpl;
import com.stnet.entity.Saleorder;
/**
 * @author Messcat
 * @version 1.1
 *
 */
public class SaleorderDaoImpl extends BaseDaoImpl implements SaleorderDao
{

    public SaleorderDaoImpl()
    {
    }

    public void delete(Saleorder saleorder)
    {
        getHibernateTemplate().delete(saleorder);
    }

    public void delete(Long id)
    {
        getHibernateTemplate().delete(get(id));
    }

    public List findAll()
    {
        List find = getHibernateTemplate().find("from Saleorder");
        return find;
    }

    public void save(Saleorder saleorder)
    {
        getHibernateTemplate().save(saleorder);
    }

    public void update(Saleorder saleorder)
    {
        getHibernateTemplate().merge(saleorder);
    }

    public Saleorder get(Long id)
    {
        return (Saleorder)getHibernateTemplate().get(Saleorder.class, id);
    }

    public List findSaleorderByAgent(Long agent){

    	List find = getHibernateTemplate().find("from Saleorder where user.id = ? order by id desc",agent);
        return find;
    	
    }
    
    public Pager getListByAgent(int pageSize, int pageNo, Long agent) {
		Session session = null;
		Pager pager = null;

		session = getHibernateTemplate().getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(Saleorder.class);

		criteria.add(Restrictions.eq("user.id", agent));
		criteria.addOrder(Order.desc("editetime"));
		int rowCount = ((Integer) criteria
				.setProjection(Projections.rowCount()).uniqueResult())
				.intValue();
		criteria.setProjection(null);
		int startIndex = pageSize * (pageNo - 1);
		criteria.setFirstResult(startIndex);
		criteria.setMaxResults(pageSize);
		List result = criteria.list();
		pager = new Pager(pageSize, pageNo, rowCount, result);

		session.close();

		return pager;
	}
}

